package com.csw.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;


/**
 * 自定义加密方式
 * @author shengwencheng
 * @Date 2021-10-30
 */

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        String password = passwordEncoder().encode("123456");
        auth
                // 使用基于内存的InMemoryUserDetailsManager
                .inMemoryAuthentication()
                //使用 PasswordEncoder 密码编码器
                //.passwordEncoder(passwordEncoder())
                //配置用户
                .withUser("chengsw").password(password).roles("admin")
                //配置其他用户
                .and()
                .withUser("chengsw2").password(password).roles("user");
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        //return NoOpPasswordEncoder.getInstance();
        return new BCryptPasswordEncoder();
    }
}
